package com.ea.holder;

import com.ea.domain.entity.UserInfo;

import java.util.Objects;

/**
 * @author NAME-L
 * @Description 登录用户自定义验证记录
 * @className UserHolderInfo
 * @date 2023-01-15 20:17:56
 */
public class UserHolderInfo {
    private static final ThreadLocal<UserInfo> userThreadLocal = new ThreadLocal<>();

    public static UserInfo getUserInfo() {
        return userThreadLocal.get();
    }

    public static void setUserInfo(UserInfo userInfo) {
        userThreadLocal.set(userInfo);
    }

    public static Long getUserId() {
        UserInfo userInfo = getUserInfo();
        return Objects.isNull(userInfo) ? 0L : userInfo.getUserId();
    }

    public static String getUserName() {
        UserInfo userInfo = getUserInfo();
        return Objects.isNull(userInfo) ? null : userInfo.getUserName();
    }

    public static void clearUserCache() {
        userThreadLocal.remove();
    }

}
